/**
 * Created by python on 19-3-5.
 */
let base_vm = new Vue({
    el: '#base_nav',
    data: {
        host: HOST,
        user_id: localStorage.user_id || sessionStorage.user_id,
        token: localStorage.token || sessionStorage.token,
        nickname: '',
        avatar_url: '',

        logging_status: false
    },
    mounted: function(){
        this.loading_nav();
    },
    methods: {
        // 获取用户基本数据
        loading_nav: function(){
            if (this.user_id && this.token) {
                axios.get(this.host + '/user/' + this.user_id + '/part/', {
                        responseType: 'json'
                    })
                    .then(response => {
                        // 加载用户数据
                        this.user_id = response.data.id;
                        this.nickname = response.data.nickname;
                        this.avatar_url = response.data.avatar_url;
                        this.logging_status = true
                    })
                    .catch(error => {
                        if (error.response.status==401 || error.response.status==403) {
                            this.logging_status = false
                        }
                    });
            } else {
                this.logging_status = false
            }
        },

        // 退出
        logout: function(){
            sessionStorage.clear();
            localStorage.clear();
            location.href = '/login.html';
        }
    }
});

